const app=getApp()

Page({
    /**
     * 页面的初始数据
     */
    data: {
      username: '',
      avatar: '',
      signature:'',
      sex: '',
      phone:'',
      age:'',
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
      var that = this
      let storageUserInfo = wx.getStorageSync('userInfo')
      this.setData({
        username: storageUserInfo.username,
        avatar: storageUserInfo.avatar,
        sex:storageUserInfo.sex,
        signature:storageUserInfo.signature,
        phone:storageUserInfo.phone,
        age:storageUserInfo.age
      })
    },
    changeAvatar() {
      var that = this 
      wx.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都
          sourceType: ['album', 'camera'], //从相册选择
          success: (res) => {
            console.log(res)
            let imgPath = res.tempFilePaths[0]
            // 上传图片
            wx.uploadFile({
              filePath: imgPath,
              name: 'file',
              // 用户信息这里
              url: app.globalData.prefixUrl + '/chong/wx/upload?userId=' + app.globalData.userId,
              header: {
                "Content-Type": "multipart/form-data"
              },
              success(e) {
                console.log("\n图片上传成功 "+e.data)
                that.setData({
                  avatar: e.data
                })
                console.log("图片更新地址：" + that.data.avatar)
              },
              fail(e){
                  console.log("\n图片上传失败: ", e)
              }
            })
            }
          }
        );
    },
    //保存按钮
    petFormSubmit:function(e){
      var that = this
      //表单返回的所有数据
      var formData=e.detail.value;
      //获取上一个页面的对象
      var pages=getCurrentPages()
      var prevPage=pages[pages.length-2]
      //调用上一个页面的setData()方法，把数据存储到上一个页面中去
      prevPage.setData({
        'userInfo.username':formData.username,
        'userInfo.avatar': that.data.avatar,
        'userInfo.sex':formData.sex,
        'userInfo.signature':formData.signature,
      })
      console.log("app.globalData.user: ", app.globalData.user)
      app.globalData.user.username =  formData.username,
      app.globalData.user.sex = formData.sex,
      app.globalData.user.age = formData.age,
      app.globalData.user.signature = formData.signature
      app.globalData.user.phone = formData.phone
      // 更新用户数据
      wx.request({
        url: app.globalData.prefixUrl + "/chong/user/userInfo/" + app.globalData.userId,
        data:{
          // id:app.globalData.userId,
           username:formData.username,
           avatar: that.data.avatar,
           sex:formData.sex,
           age:formData.age,
           signature:formData.signature,
           phone:formData.phone,
        },
        method:"PUT",
        header:{
          'Content-type': "application/x-www-form-urlencoded"
        },
      // 更新storage
        success(e){
         wx.setStorageSync('userInfo', app.globalData.user) 
        }
      })
      //返回上一个页面
      wx.navigateBack()
    }
  })
  